/**
 * Create By FengZheng
 * On 2019/8/27 20:31
 */
(function ($) {
    "use strict";

    let $dom = {
        $navHead: $("#console-nav"),
        $msgBtn: $("#message"),
        $msgBox: $("#messageContainer"),
        $asideBar: $("#aside-bar"),
        $frame: $("#console-frame"),
        $yundaoLogo: $("#yundao-logo")
    };

    let system = {
        projectArray: [],
        classesArray: []
    };

    /**
     * 顶部logo点击
     */
    $dom.$yundaoLogo.on("click",function () {
        changeIframe("/student/frame/index");
    });


    /**
     * 侧边栏点击
     */
    $dom.$asideBar.on("click",function (ev) {
        let event = ev || window.event;
        let target = event.target || event.srcElement;

        let liDom = $(target).parents("li");
        // 父级元素
        if(liDom.attr("data-url") === undefined){
            let aDom = liDom.children("a");
            if(!aDom.hasClass("active")){
                aDom.addClass("active");

                system.projectArray.push(aDom);
                if(system.projectArray.length > 1){
                    let lastDom = system.projectArray.shift();
                    lastDom.removeClass("active");
                }
            }
        }
        // 子元素
        else{
            let aDom = $(liDom[0]).children("a");
            let dataUrl = $(liDom[0]).attr("data-url");
            console.log(dataUrl);

            if(!aDom.hasClass("active")){
                aDom.addClass("active");

                system.classesArray.push(aDom);
                if(system.classesArray.length > 1){
                    let lastDom = system.classesArray.shift();
                    lastDom.removeClass("active");
                }
            }

            // 子元素对应的父级元素
            let parentADom = $(liDom[1]).children("a");
            if(!parentADom.hasClass("active")){
                parentADom.addClass("active");

                system.projectArray.push(parentADom);
                if(system.projectArray.length > 1){
                    let lastDom = system.projectArray.shift();
                    lastDom.removeClass("active");
                }
            }

            let url = "/student/frame/" + dataUrl;
            changeIframe(url);
        }
    });

    /**
     * 顶部消息通知点击
     */
    $dom.$msgBtn.on("click",function () {
        let parentDom = $(this).parent("li");
        let childDom = parentDom.children("div");
        if(parentDom.hasClass("show")){
            parentDom.removeClass("show");
            childDom.removeClass("show");
        }else{
            parentDom.addClass("show");
            childDom.addClass("show");
        }
    });

    /**
     * 鼠标移出消息通知区域
     */
    $dom.$msgBox.on("mouseout",function () {
        if($dom.$msgBox.hasClass("show")){
            let msgTimer = window.setTimeout(function () {
                $dom.$msgBox.removeClass("show");
                $dom.$msgBtn.parent("li").removeClass("show");
                window.clearTimeout(msgTimer);
            },2000);
        }
    });

    /**
     * 设置iframe大小
     */
    function setFrameSize() {
        let navHeight = $dom.$navHead.height();
        let screenHeight = $(document).height();

        let frameHeight = screenHeight - navHeight - 25;
        $dom.$frame.height(frameHeight);
    }

    $(document).ready(function () {
        setFrameSize();
    });
}(window.jQuery));